﻿#include "Wall.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/ServerPackets.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    Wall::Wall(MonsterInfo *info) : MonsterObject(info)
    {
        setDirection(MirDirection::Up);
    }

    void Wall::Despawn()
    {
        MonsterObject::Despawn();
    }

    bool Wall::getCanAttack() const
    {
        return false;
    }

    bool Wall::getCanMove() const
    {
        return false;
    }

    void Wall::Turn(MirDirection dir)
    {
    }

    bool Wall::Walk(MirDirection dir)
    {
        return false;
    }

    int Wall::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        CheckDirection();

        if (!Conquest->getWarIsOn() || attacker->MyGuild != nullptr && Conquest->Owner == attacker->MyGuild->Guildindex)
        {
            damage = 0;
        }

        return MonsterObject::Attacked(attacker, damage, type, damageWeapon);
    }

    int Wall::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        CheckDirection();

        if (!Conquest->getWarIsOn())
        {
            damage = 0;
        }

        return MonsterObject::Attacked(attacker, damage, type);
    }

    void Wall::RepairWall()
    {
        if (HP == 0)
        {
            Revive(Stats[Stat::HP], false);
        }
        else
        {
            SetHP(Stats[Stat::HP]);
        }

        CheckDirection();
    }

    void Wall::CheckDirection()
    {
        MirDirection newDirection = static_cast<MirDirection>(4 - GetDamageLevel());

        if (newDirection != getDirection())
        {
            setDirection(newDirection);
            S::ObjectTurn *tempVar = new S::ObjectTurn();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void Wall::Die()
    {
        MonsterObject::Die();
    }

    int Wall::GetDamageLevel()
    {
        int level = static_cast<int>(BankersRounding::round(static_cast<double>(4 * HP) / Stats[Stat::HP]));

        if (level < 1)
        {
            level = 1;
        }

        return level;
    }

    bool Wall::getCanRegen() const
    {
        return false;
    }
}
